from nest.path import Path
from nest.path.exception import (PathError, MissingParser)


class PathIterator(Path):

    def __init__(self, parser):
        self.__parser = parser
        self.__shallow_parser = parser.copy()

    def get_parser(self):
        return self.__parser

    def __iter__(self):
        self.__parser = self.__shallow_parser
        self.__shallow_parser = self.__parser.copy()
        return self

    def next(self):
        if not self.__parser.get_head():
            raise StopIteration()
        return PathKey(self.__parser.pop_head())


class PathKey(PathIterator):
    '''
    Immutable path iterator, containing a single path key.
    '''

    def __init__(self, path):
        self.__path = path
        self.__iterated = False
        self.__parser = None

    def __iter__(self):
        return PathKey(self.__path)

    def next(self):
        if not self.__iterated:
            self.__iterated = True
            return self.__path
        raise StopIteration()

    def get_path(self):
        return self.__path

    def __str__(self):
        return '%s' % self.__path

    def __repr__(self):
        return '\'%s\'' % self.__path
